<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\Exception;
use think\Model;

/**
 * @mixin \think\Model
 */
class GoodsProp extends Model
{
    //
    protected $pk = 'pid';
    protected $error;
    public function getError()
    {
        return $this->error;
    }
    //属性修改器
    public function setPvaluesAttr($value)
    {
        if(is_array($value)){
            return implode(',',$value);
        }
        return $value;
    }
    public function searchKeywordsAttr($query,$value,$data){
        if ('' !== $value){
            $query->whereLike('pname','%'.$value.'%');
        }
    }
    public function searchPropValueAttr($query,$value)
    {
        if(isset($value) && is_array($value) && count($value)>0){
            foreach ($value as $v){
                $query->where('pvalues','find in set',$v);
            }
        }
    }
    public function getShanchuAttr($value)
    {
        return $value?'是':'否';
    }
    public function add(array $data)
    {
        if(''!==$data['pvalues']){
            $zhi=implode($data['pvalues'],',');
            $data['pvalues']=$zhi;
        }

        try{
            if(empty($data)){
                $this->error = '数据不能为空!';
            }
            $this->save($data);
            $id = $this->toArray();
            return $id[$this->pk];
        }catch (\Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
    public static function getProp($data=[],$query=[],$lent=[],$page=10)
    {
        if (isset($data['page'])){
            unset($data['page']);
        }

        $field = [
            'pid','pname','pvalues','status','sort_order','shanchu',
        ];
        $model = self::field($field)->order('sort_order')
            ->withSearch(array_keys($data),$data);
        $list = $model;
        $arr=[
            'list_rows'=>$page,
            'query'=>$query,
        ];
        $fenye=$list->paginate($arr)->render();

        $fen=[
            'list_rows'=>$page,
            'query'=>$query,
        ];

        $list=$list->paginate($fen)->toArray();

        foreach ($list['data'] as $k => $v) {
            $v['pvalues'] = explode(',', $v['pvalues']);
            $arr=[];
            foreach ($v['pvalues'] as $vv) {
                foreach ($lent['prop_value_list'] as $value) {
                    if ($vv == $value['vid']) {
                        $arr[] = $value['prop_value'];
                    }
                }
            }
            $str =implode(',',$arr);
            $list['data'][$k]['priv_v']=$str;
        }
        //返回值
        return [
            'lent'   => $list,
            'fenye' => $fenye,
        ];
    }
    public function upt($data, $where = [],$allowField = []){
        try{
            if (empty($data)){
                $this->error = "修改数据不能为空";
                return ;
            }

            if (!empty($allowField)) {
                $this->allowField($allowField);
            }
            if (!empty($where)){
                $this->setUpdateWhere($where);

                if (isset($data[$this->pk]))   unset($data[$this->pk]);
            }

            return $this->exists(true)->save($data);
        }catch (Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
}
